/*
 * @(#)XmlResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result.template;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.AbstractResult;
import com.nhncorp.usf.core.result.direct.XmlResult;
import com.opensymphony.xwork.ActionContext;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * {@link com.nhncorp.usf.core.result.direct.HtmlResult} TestCase.
 *
 * @author Web Platform Development Team
 */
public class XmlResultTestCase {
    AbstractResult result = new XmlResult();
    //ResultPageInfo resultPageInfo;

    @BeforeClass
    public static void initialize() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);

        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Test
    public void executeXmlResult() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "");
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "xml");
        resultPageInfo.getProperties().setProperty("src", "result,subMap1,subList1,subList2");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();

        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("result", "abc");
        Map<String, Object> subMap1 = new HashMap<String, Object>();
        subMap1.put("subMap1-1", "1");
        subMap1.put("subMap1-2", "2");
        Map<String, Object> subMap2 = new HashMap<String, Object>();
        subMap2.put("subMap1-3-1", "3");
        subMap2.put("subMap1-3-2", "4");
        subMap1.put("subMap1-3", subMap2);
        dataMap.put("subMap1", subMap1);

        List<Map<String, Object>> subList1 = new ArrayList<Map<String, Object>>();
        List<List<String>> subList2 = new ArrayList<List<String>>();
        Map<String, Object> subListMap1 = new HashMap<String, Object>();
        Map<String, Object> subListMap2 = new HashMap<String, Object>();
        List<String> subListMap2List = new ArrayList<String>();
        subListMap1.put("subListMap1-1", "1234");
        subListMap1.put("subListMap1-2", "2345");
        subListMap2.put("subListMap2-1", "1234");
        subListMap2.put("subListMap2-2", "2345");
        subListMap2List.add("subListMap2List-1");
        subListMap2List.add("subListMap2List-2");
        subListMap2.put("subListMap2List", subListMap2List);
        subList1.add(subListMap1);
        subList1.add(subListMap2);

        List<String> subList2List = new ArrayList<String>();
        subList2List.add("subList2List1");
        subList2List.add("subList2List2");
        subList2.add(subList2List);

        dataMap.put("subList1", subList1);
        dataMap.put("subList2", subList2);

        try {
            result.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<message type=\"response\" service=\"koreabusinesssupport.developmentplatform.libraryandframework.USF\" version=\"1.0.0\" xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://dev.naver.com/xsd/usf/usf-xml-result.xsd\">\n" +
                    "<result>\n" +
                    "\t<data name=\"result\" type = \"String\" ><![CDATA[abc]]></data>\n" +
                    "\t<data name=\"subMap1\" type = \"HashMap\" >\n" +
                    "\t\t<data name=\"subMap1-1\" type = \"String\" ><![CDATA[1]]></data>\n" +
                    "\t\t<data name=\"subMap1-2\" type = \"String\" ><![CDATA[2]]></data>\n" +
                    "\t\t<data name=\"subMap1-3\" type = \"HashMap\" >\n" +
                    "\t\t\t<data name=\"subMap1-3-2\" type = \"String\" ><![CDATA[4]]></data>\n" +
                    "\t\t\t<data name=\"subMap1-3-1\" type = \"String\" ><![CDATA[3]]></data>\n" +
                    "\t\t</data>\n" +
                    "\t</data>\n" +
                    "\t<data name=\"subList1\" type = \"ArrayList\" >\n" +
                    "\t\t<item type=\"map\">\n" +
                    "\t\t\t<data name=\"subListMap1-1\" type = \"String\" ><![CDATA[1234]]></data>\n" +
                    "\t\t\t<data name=\"subListMap1-2\" type = \"String\" ><![CDATA[2345]]></data>\n" +
                    "\t\t</item>\n" +
                    "\t\t<item type=\"map\">\n" +
                    "\t\t\t<data name=\"subListMap2-1\" type = \"String\" ><![CDATA[1234]]></data>\n" +
                    "\t\t\t<data name=\"subListMap2-2\" type = \"String\" ><![CDATA[2345]]></data>\n" +
                    "\t\t\t<data name=\"subListMap2List\" type = \"ArrayList\" >\n" +
                    "\t\t\t\t<item type=\"String\"><![CDATA[subListMap2List-1]]></item>\n" +
                    "\t\t\t\t<item type=\"String\"><![CDATA[subListMap2List-2]]></item>\n" +
                    "\t\t\t</data>\n" +
                    "\t\t</item>\n" +
                    "\t</data>\n" +
                    "\t<data name=\"subList2\" type = \"ArrayList\" >\n" +
                    "\t\t<item type=\"list\">\n" +
                    "\t\t\t<item type=\"String\"><![CDATA[subList2List1]]></item>\n" +
                    "\t\t\t<item type=\"String\"><![CDATA[subList2List2]]></item>\n" +
                    "\t\t</item>\n" +
                    "\t</data>\n" +
                    "</result>\n" +
                    "</message>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }
}